package com.yunxi.service.localService.impl.sat;

import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sat.SatMessMapper;
import com.yunxi.model.sat.MsgTemplate;
import com.yunxi.model.sat.SatMess;
import com.yunxi.sat.pojo.SatMessAddVO;
import com.yunxi.service.localService.JpushForBizService;
import com.yunxi.service.localService.sat.MsgTemplateService;
import com.yunxi.service.localService.sat.SatMessService;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-07-16
 */
@Service
public class SatMessServiceImpl extends ServiceImplPlus<SatMessMapper, SatMess> implements SatMessService {

   private static final Logger logger = LoggerFactory.getLogger(SatMessServiceImpl.class);

   @Autowired
   @Qualifier(value = "msgTemplateServiceImpl")
   MsgTemplateService msgTemplateService;

   @Autowired
   @Qualifier(value = "jpushForBizServiceImpl")
   JpushForBizService jpushForBizService;


   @Autowired
   SatMessService SatMessServiceImpl;

   /**
    * 新增消息
    *
    * @param satMessAddVO
    */
   @Override
   public void newMess(SatMessAddVO satMessAddVO) throws InvocationTargetException, IllegalAccessException {
      QueryWrapper<MsgTemplate> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("TYPE", satMessAddVO.getTemplateType());
      MsgTemplate msgTemplate = msgTemplateService.getOne(queryWrapper);
      if (msgTemplate != null) {
         satMessAddVO.setInfo(msgTemplate.getInfo());
      }
      SatMess satMess = new SatMess();
      BeanUtils.copyProperties(satMess, satMessAddVO);
      baseMapper.insert(satMess);

      //消息推送
      if (StringUtils.isNotEmpty(satMess.getSalesConsultantNo()) && StringUtils.isNotEmpty(satMess.getReceiveRoleCode()) && StringUtils.isNotEmpty(satMess.getInfo())) {
         int send = jpushForBizService.send(satMess.getSalesConsultantNo(), satMess.getReceiveRoleCode(), satMess.getInfo());
         logger.debug("=======================消息推送结果=============" + send);
      }
   }

   /**
    * 新增消息
    *
    * @param
    */
   @Override
   public void newMess2(List<SatMess> satMessList) throws InvocationTargetException, IllegalAccessException {
      SatMessServiceImpl.saveBatch(satMessList);
      //消息推送
      List<String> salesConsultantNos = new ArrayList<>();
      List<String> receiveRoleCodes = new ArrayList<>();
      for (SatMess satMess : satMessList) {
         salesConsultantNos.add(satMess.getSalesConsultantNo());
         receiveRoleCodes.add(satMess.getReceiveRoleCode());

      }
      if (salesConsultantNos != null && salesConsultantNos.size() > 0 && receiveRoleCodes != null && receiveRoleCodes.size() > 0) {
         String info = satMessList.get(0).getInfo();
         int send = jpushForBizService.send(salesConsultantNos, receiveRoleCodes, info);
         logger.debug("=======================消息推送结果=============" + send);

      }
   }

   /**
    * PDA新增消息
    *
    * @param satMessAddVO
    */
   @Override
   public void pdaNewMess(SatMessAddVO satMessAddVO) throws InvocationTargetException, IllegalAccessException {
      SatMess satMess = new SatMess();
      BeanUtils.copyProperties(satMess, satMessAddVO);
      baseMapper.insert(satMess);

      //消息推送
      if (StringUtils.isNotEmpty(satMess.getSalesConsultantNo()) && StringUtils.isNotEmpty(satMess.getReceiveRoleCode()) && StringUtils.isNotEmpty(satMess.getInfo())) {
         int send = jpushForBizService.sendToPda(satMess.getSalesConsultantNo(), satMess.getReceiveRoleCode(), satMess.getInfo());
         logger.debug("=======================pda消息推送结果=============" + send);
      }
   }
}
